// pages/PointsExchange/PointsExchange.js
var that,
  api = new (require('../../utils/api.js')),
  headimg = api.getimgurl(),
  app = getApp(),
  util = new (require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectindex:0,
    userscore:0,
    Couponslist:[
    //   {
    //     id:1,
    //     price:5,
    //     point:500,
    //     iscan:0
    //   },
    //   {
    //     id:2,
    //     price:5,
    //     point:1000,
    //     iscan:1
    //   },
    //   {
    //     id:3,
    //     price:5,
    //     point:2000,
    //     iscan:1
    //   },
    // ],
    // integralList:[
    //   {
    //     consume:200,
    //     point:20,
    //     time:'2020.6.1',
    //     state:0
    //   },
    //   {
    //     consume:200,
    //     point:20,
    //     time:'2020.6.1',
    //     state:0
    //   },
    //   {
    //     consume:200,
    //     point:20,
    //     time:'2020.6.1',
    //     state:0
    //   },
    //   {
    //     consume:200,
    //     point:20,
    //     time:'2020.6.1',
    //     state:0
    //   },
    //   {
    //     consume:200,
    //     point:20,
    //     time:'2020.6.1',
    //     state:0
    //   },
    //   {
    //     consume:'兑换加油券',
    //     point:20,
    //     time:'2020.6.1',
    //     state:1
    //   },
    ],
    ischeck:!1,
    isconfirm:!1,
    integralList:[],
    couponId:0,
    exchangescore:0,
    nextPage:0,
    page:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this
    wx.setNavigationBarTitle({
      title: '积分兑换'
    })
    that.getdata()
  },
  getdata(){
    wx.showLoading({
      title: '加载中',
    })
    api.userscore(res=>{
      if(res.data.code==0){
        that.setData({userscore:res.data.entity.data.score},()=>{
          that.getscoreitems()
        })
      }else{
        wx.hideLoading()
      }
    })
  },
  //积分兑换列表
  getscoreitems(){
    api.scoreitems(res=>{
      wx.hideLoading()
      if(res.data.code==0){
        that.setData({Couponslist:res.data.entity.data})
      }
    })
  },
    //积分记录列表
    getscorerecord(){
      let {page,integralList}=that.data
      api.scorerecord({page,cateId:1},res=>{
        wx.hideLoading()
        if(res.data.code==0){
          let {infoArr,nextPage}=res.data.entity.data
          if(infoArr.length>0){
            infoArr.forEach(x=>{
              x.date=util.formatDate(new Date(x.addTime*1000))
              integralList.push(x)
            })
          }
          that.setData({integralList,nextPage})
        }
      })
    },
  toselect(e){
    let { index } = e.currentTarget.dataset, { selectindex } = that.data;
    if (selectindex == index) {
      return;
    } 
    wx.showLoading({
      title: '加载中',
    })
    if(index==0){
      wx.setNavigationBarTitle({
        title: '积分兑换'
      })
      that.setData({selectindex: index},()=>{
        that.getscoreitems()
      })
    }else{
      wx.setNavigationBarTitle({
        title: '兑换记录'
      })
      this.setData({ selectindex: index,page:1,integralList:[]},()=>{
        that.getscorerecord()
      });
    }
 
  },
  tomyPointsRecord(){
    console.log(1111111);
    
    wx.navigateTo({
      url: '../myPointsRecord/myPointsRecord',
    })
  },
  exchange(e){
    let {Couponslist}=that.data,{id}=e.currentTarget.dataset
    Couponslist.forEach(x=>{
      if(x.couponId==id){
        that.setData({couponId:id,exchangescore:x.score,isconfirm:!0})
      }
    })

    // that.setData({isconfirm:!0})
  },
  close(){
    that.setData({ischeck:!1})
  },
  confirm(){
    let {couponId}=that.data
    api.scoreapply({couponId},res=>{
      if(res.data.code==0){
        that.setData({ischeck:!0,isconfirm:!1},()=>{
          that.getdata()
        })
      }else{
        that.setData({ischeck:!1,isconfirm:!1})
        wx.showToast({
          title: res.data.message,
          icon: 'none'
        })
      }
    })
  },
  cancel(){
    that.setData({isconfirm:!1,ischeck:!1})
  },
  tocoupons(){
    wx.navigateTo({
      url: '../coupons/coupons',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    that.setData({isconfirm:!1,ischeck:!1})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let {selectindex,page,nextPage}=that.data
    if(selectindex==1&&nextPage==1){
      wx.showLoading({
        title: '加载中',
      })
      page=page+1
      that.setData({page},()=>{
        that.getscorerecord()
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})